package com.example.librarymanagement.mapper;

import com.example.librarymanagement.model.BorrowRecord;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface BorrowRecordMapper {
    @Insert("INSERT INTO borrowing_records (book_id, user_id, borrow_date) " +
            "VALUES (#{bookId}, #{userId}, #{borrowDate})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertBorrowRecord(BorrowRecord record);

    @Select("SELECT br.id, br.book_id AS bookId, b.book_name AS bookName, b.author AS author, br.borrow_date AS borrowDate, br.return_date AS returnDate " +
            "FROM borrowing_records br " +
            "INNER JOIN books b ON br.book_id = b.id " +
            "WHERE br.user_id = #{userId} " +
            "AND br.return_date IS NULL")
    List<BorrowRecord> selectByUserId(Integer userId);

    // 更新归还日期
    @Update("UPDATE borrowing_records SET return_date = #{returnDate} WHERE id = #{recordId}")
    void updateReturnDate(@Param("recordId") Integer recordId, @Param("returnDate") Date returnDate);
    @Select("SELECT " +
            "br.id as id, " +
            "br.book_id as bookId, " +
            "br.user_id as userId, " +
            "br.borrow_date as borrowDate, " +
            "br.return_date as returnDate, " +
            "b.book_name as bookName, " +
            "b.category as category, " +
            "b.author as author, " +
            "u.username as username " +
            "FROM borrowing_records br " +
            "JOIN books b ON br.book_id = b.id " +
            "JOIN users u ON br.user_id = u.id")
    List<BorrowRecord> selectAllWithDetails();
    @Select("SELECT " +
            "br.id as id, " +
            "br.book_id as bookId, " +
            "br.user_id as userId, " +
            "br.borrow_date as borrowDate, " +
            "br.return_date as returnDate, " +
            "b.book_name as bookName, " +
            "b.category as category, " +
            "b.author as author, " +
            "u.username as username " +
            "FROM borrowing_records br " +
            "JOIN books b ON br.book_id = b.id " +
            "JOIN users u ON br.user_id = u.id " +
            "WHERE br.user_id = #{userId}")
    List<BorrowRecord> selectBByUserId(@Param("userId") Integer userId);
}